<?php

class I18n
{
	static private $_locale = null;

	static private $_language;

	static public function __init()
	{
		$lang = 'vi';
		$locale = 'vi_vn';

		if (isset($_COOKIE['locale']))
		{
			$locale = $_COOKIE['locale'];
		}

		if (isset($_COOKIE['language']))
		{
			$lang = $_COOKIE['lang'];
		}
		self::$_locale = $locale;
		self::$_language = $lang;

	}

	/**
	 * @param string $locale
	 * @return I18n_Locale
	 */
	public static function getLocale($locale)
	{
		static $locales = array();

		$locale = strtolower(str_replace('-', '_', $locale));

		if (!isset($locales[$locale]))
		{
			require_once 'i18n/locale.php';
			$locales[$locale] = new I18n_Locale($locale ? $locale : self::getCurrentLocale());
		}

		return $locales[$locale];
	}

	public static function getMessage($lang)
	{
		static $messages = array();

		$lang = strtolower($lang);

		if (!isset($messages[$lang]))
		{
			require_once 'i18n/locale.php';
			$messages[$lang] = new I18n_Message($lang ? $lang : self::getCurrentLanguage());
		}

		return $messages[$lang];
	}

	/**
	 * @return array IDs of the locales which the framework can recognize
	 */
	public static function getLocales()
	{
		static $locales;

		if ($locales === null)
		{
			$locales = array();
			$dataPath = dirname(__FILE__) . '/i18n/ldml';
			$folder = @opendir($dataPath);
			while (($file = @readdir($folder)) !== false)
			{
				$fullPath = $dataPath . DIRECTORY_SEPARATOR . $file;
				if (substr($file, -4) === '.php' && is_file($fullPath))
					$locales[] = substr($file, 0, -4);
			}
			closedir($folder);
			sort($locales);
		}
		return $locales;
	}

	/**
	 * get locale string
	 * @return string
	 */
	static public function getCurrentLocale()
	{
		return self::$_locale;
	}

	/**
	 * get locale string
	 * @return string
	 */
	static public function getCurrentLanguage()
	{
		return self::$_language;
	}

	static public function getAllLanguages()
	{
		static $languages = null;

		if (null == $languages)
		{
			$cache = Cache::instance();
			if (false == ($languages = $cache -> get('core/i18n/language/getAllLanuages')))
			{
				$languages = Model::getTable('core/i18n/language') -> getAllLanguages();
				$cache -> set('languages', $languages);
			}
		}

		return $languages;
	}

	static public function getAllCountries()
	{
		return Config::readFile('country');
	}

	static public function getAllTimezones()
	{
		return Config::readFile('timezone');
	}

	static public function getAllLocales()
	{
		return Config::readFile('locale');
	}

	static public function getAllCurrencies()
	{
		static $currencies = null;

		if (null == $currencies)
		{
			$cache = Cache::instance();
			if (false == ($currencies = $cache -> get('core/i18n/language/getAllCurrencies')))
			{
				$currencies = Model::getTable('core/i18n/currency') -> getAllCurrencies();
				$cache -> set('currencies', $currencies);
			}
		}

		return $currencies;
	}

}

function __($message, $values = NULL)
{
	if (NULL == $values)
	{
		return $message;
	}
	return strtr($message, $values);
}

/**
 * @param   string  $string text to translate
 * @param   array   $values values to replace in the translated text
 * @param   string  $numeric source language
 * @return  string
 */
function __text($message, array $values = NULL, $plural = FALSE, $lang = NULL)
{

	return I18n::getMessage($lang) -> formatMessage($message, $values, $plural);
}

function __number($value, $opt = NULL, $locale = NULL)
{
	return I18n::getLocale($locale) -> formatDecimal($value);
}

function __percentage()
{
	return I18n::getLocale($locale) -> formatDateTime($value);
}

function __date($value, $type = 'medium', $locale = NULL)
{
	return I18n::getLocale($locale) -> formatDateTime($value, $type);
}

function __currency($value, $opt = NULL, $locale = NULL)
{
	return I18n::getLocale($locale) -> formatCurrency($value);
}
